package com.zfzs.post.modules.post.service.impl;

import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.modules.post.domain.PostTerminalDistribu;
import com.zfzs.post.modules.post.repository.PostTerminalDistribuRepository;
import com.zfzs.post.modules.post.service.PostTerminalDistribuService;
import com.zfzs.post.modules.post.service.dto.PostTerminalDistribuQueryCriteria;
import com.zfzs.post.modules.post.service.mapper.PostTerminalDistribuMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;

/**
* @author wq
* @date 2019-10-09
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PostTerminalDistribuServiceImpl implements PostTerminalDistribuService {

    @Autowired
    private PostTerminalDistribuRepository postTerminalDistribuRepository;

    @Autowired
    private PostTerminalDistribuMapper postTerminalDistribuMapper;

    @Override
    public Object queryAll(PostTerminalDistribuQueryCriteria criteria, Pageable pageable){
        Page<PostTerminalDistribu> page = postTerminalDistribuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(postTerminalDistribuMapper::toDto));
    }

    @Override
    public Object queryAll(PostTerminalDistribuQueryCriteria criteria){
        return postTerminalDistribuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
    }

    @Override
    public PostTerminalDistribu findById(Long id) {
        Optional<PostTerminalDistribu> postTerminalDistribu = postTerminalDistribuRepository.findById(id);
        ValidationUtil.isNull(postTerminalDistribu,"PostTerminalDistribu","id",id);
        return postTerminalDistribu.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PostTerminalDistribu create(PostTerminalDistribu resources) {
        resources.setReceiveState(1);
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return postTerminalDistribuRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PostTerminalDistribu resources) {
        Optional<PostTerminalDistribu> optionalPostTerminalDistribu = postTerminalDistribuRepository.findById(resources.getDistribuId());
        ValidationUtil.isNull( optionalPostTerminalDistribu,"PostTerminalDistribu","id",resources.getDistribuId());
        PostTerminalDistribu postTerminalDistribu = optionalPostTerminalDistribu.get();
        postTerminalDistribu.copy(resources);
        postTerminalDistribuRepository.save(postTerminalDistribu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        postTerminalDistribuRepository.deleteById(id);
    }

    @Override
    public List<PostTerminalDistribu> getTerDistribuListBySnList(List<String> snList) {
        return postTerminalDistribuRepository.findBySnInAndState(snList,2);
    }

    @Override
    public void updateDisList(List<PostTerminalDistribu> list) {
        if(list.stream().anyMatch(e -> e.getDistribuId() == null)){
            throw new BadRequestException("更新分配异常:id不能为空");
        }
        postTerminalDistribuRepository.saveAll(list);
    }

    @Override
    public Long findOwnerBySn(String sn) {
        PostTerminalDistribu dis = postTerminalDistribuRepository.findBySnAndState(sn,2);
        return  dis.getAgentId();
    }


}